package com.itheima.wemedia.controller;


import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.DownOrUpDto;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.WmNewsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import com.itheima.core.controller.AbstractCoreController;

/**
 * @description <p>自媒体图文内容信息</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.controller
 */
@Api(value="WmNewsController",tags = "自媒体图文内容信息")
@RestController
@RequestMapping("/news")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService=wmNewsService;
    }

    /**
     * 文章分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public PageResultVo<WmNews> findPage(@RequestBody WmNewsPageReqDto dto){
        PageResultVo<WmNews> vo = wmNewsService.findPage(dto);
        return vo;
    }

    /**
     * 发表文章
     * @param draft
     * @param dtoSave
     * @return
     */
    @PostMapping("/submit")
    public ResultVo submit(Boolean draft, @RequestBody WmNewsDtoSave dtoSave){
        wmNewsService.submit(draft,dtoSave);
        return ResultVo.ok("操作成功！");
    }

    /**
     * 通过id查询自己发布的文章
     * @param wmNewsId
     * @return
     */
    @GetMapping("/one/{wmNewsId}")
    public ResultVo<WmNews> getById(@PathVariable(value = "wmNewsId") Long wmNewsId){
        WmNews wmNews = wmNewsService.getMyWmNewsById(wmNewsId);
        return ResultVo.ok(wmNews);
    }

    /**
     * 通过id删除文章，只能删除自己的，已上架的文章不能删除
     * @param wmNewsId
     * @return
     */
    @GetMapping("/del_news/{wmNewsId}")
    public ResultVo deleteById(@PathVariable(value = "wmNewsId") Long wmNewsId){
        wmNewsService.deleteById(wmNewsId);
        return ResultVo.ok("操作成功!");
    }

    /**
     * 文章上下架操作
     * @param dto
     * @return
     */
    @PostMapping("/down_or_up")
    public ResultVo downOrUp(@RequestBody DownOrUpDto dto){
        wmNewsService.downOrUp(dto);
        return ResultVo.ok("操作成功!");
    }
}

